package com.aboo.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author Yata Aboo
 * @Date 2012-11-27
 * @Desc 加解码工具包
 */
public class EncoderUtil {
	
	/**
	 * 对字符串进行编码，编码方式为enc，例如UTF-8
	 * @param str
	 * @param enc
	 * @return
	 */
	public static String encode(String str, String enc) {
		try {
			return URLEncoder.encode(str, enc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 对字符串进行解码，解码方式为enc，例如UTF-8
	 * @param str
	 * @param enc
	 * @return
	 */
	public static String decode(String str, String enc) {
		try {
			return URLDecoder.decode(str, enc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 对字符串进行UTF-8编码
	 * @param str
	 * @return
	 */
	public static String encodeUTF8(String str) {
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 对字符串进行UTF-8解码
	 * @param str
	 * @return
	 */
	public static String decodeUTF8(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
